home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Simulation / PDP-8 Simulator / Source Code / Assembler / Source.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-09-13  |  4.4 KB  |  237 lines  |  [TEXT/KAHL]

  1. /************************************************************
  2. *
  3. *
  4. *    Unit to look after source code.
  5. *
  6. *    by Adrian Bool in cooperation with Graham Cox.
  7. *
  8. *    copyright © phantasm coding 1992.
  9. *
  10. *
  11. ************************************************************/
  12.  
  13. #include "Global.h"
  14. #include "Source.h"
  15.  
  16. #include "Source.proto.h"
  17.  
  18. /*   public   */
  19.  
  20. sHandle newSource(void) 
  21.     {    
  22.     sHandle        temp;
  23.     
  24.     temp = (sHandle) NewHandle(sizeof(source));
  25.     
  26.     if (temp != nil)
  27.         {
  28.         (*temp)->storage = nil;
  29.         (*temp)->caret = 0;
  30.         }
  31.     return(temp);
  32.     }
  33.     
  34. void disposeSource(sHandle theSource)
  35.     {
  36.     if (theSource != nil)
  37.         {
  38.         if ((*theSource)->storage != nil)
  39.             DisposHandle((Handle) (*theSource)->storage);
  40.         
  41.         DisposHandle((Handle) theSource);
  42.         }
  43.     }    
  44.     
  45. void loadSource(sHandle sourceCode)
  46.     {
  47.     short fileHandle;
  48.     long size,x;
  49.     sStHandle storage;
  50.     SFReply    fSpec;
  51.     OSErr    theErr;
  52.     int        fPath;
  53.     
  54.     if (sourceCode != nil)
  55.         {
  56.         fSpec = (*sourceCode)->fileSpec;
  57.         
  58.         if (! FSOpen(&fSpec.fName,fSpec.vRefNum,&fPath))
  59.             {
  60.             if (! GetEOF(fPath,&size))
  61.                 {
  62.                 size++;                                /* make room for terminating 0 */
  63.  
  64.                 storage = (sStHandle) NewHandle(size);
  65.                 if (storage != nil)
  66.                     {
  67.                     HLock((Handle) storage);
  68.                     theErr = FSRead(fPath,&size,*storage);
  69.                     HUnlock((Handle) storage);
  70.                     if (!theErr)
  71.                         AsmError = FileOperationFailed;
  72.                     }
  73.                 else
  74.                     AsmError = NotEnoughMemory;
  75.                 }
  76.             else
  77.                 AsmError = FileOperationFailed;
  78.                     
  79.             theErr = FSClose(fPath);
  80.             }
  81.             else
  82.                 AsmError = FileOperationFailed;
  83.     
  84.         (*storage)[size] = '\0';
  85.     
  86.         for(x=0 ; x <= size ; x++) (*storage)[x] = tolower((*storage)[x]);
  87.     
  88.         (*sourceCode)->size = size;
  89.         (*sourceCode)->storage = storage;
  90.         }
  91.     }
  92.     
  93.     
  94. void getSection(pHandle theProgram , char *string)
  95.     {
  96.     int x = 0;
  97.     char ch;
  98.     char **startHandle;
  99.     long caretPos;
  100.     sHandle sourceCode;
  101.     
  102.     sourceCode = (*theProgram)->sourceCode;
  103.     startHandle = (*sourceCode)->storage; 
  104.     caretPos = (*sourceCode)->caret;
  105.     
  106.     while(isspace(ch = (*startHandle)[caretPos]) && (caretPos <= (*sourceCode)->size))
  107.         {
  108.         if (ch == '\r') (*sourceCode)->line++;
  109.         caretPos++;
  110.         }
  111.     
  112.     if ((*startHandle)[caretPos] == '\0')
  113.         AsmError = UnexpectedEndOfFile;    
  114.     else
  115.         {
  116.         ch = (*startHandle)[caretPos];
  117.         while(isgraph(ch) && (ch != '\0') && (x <= 255))
  118.             {
  119.             string[x] = ch;
  120.             x++; caretPos++;
  121.             ch = (*startHandle)[caretPos];
  122.             }
  123.             
  124.         if (x > 255)
  125.             AsmError = SectionTooLong;
  126.         else 
  127.             string[x] = 0;
  128.         }
  129.     (*sourceCode)->caret = caretPos;
  130.     }
  131.     
  132.     
  133. short getSegment(char *theSection , short *position , str255 theResult)
  134.     {
  135.     short x = 0;
  136.     
  137.     while(theSection[*position] == ',') (*position)++;    /* get to next data item */
  138.     
  139.     while((theSection[*position] != ',') && (theSection[*position] != '\0')) /* read in item */ 
  140.         {
  141.         theResult[x] = theSection[*position];
  142.         (*position)++; x++;
  143.         }
  144.         
  145.     theResult[x] = '\0';                                /* terminate string */
  146.     
  147.     if (theResult[0] == '\0') return(false);
  148.     else return(true);
  149.     }
  150.             
  151. operator getPart(char *theSegment , short *position , str255 theResult)
  152.     {
  153.     short x = 0;
  154.     operator theOperator;
  155.     char ch;
  156.     
  157.     if (theSegment[*position] == '\0')
  158.         {
  159.         theResult[0] = '\0';
  160.         return(End);
  161.         }
  162.         
  163.     ch = theSegment[*position];
  164.     
  165.     if (isalnum(ch) || (ch == '%') || (ch == '$')) 
  166.         theOperator = None;
  167.     else
  168.         {
  169.         switch (ch)
  170.             {
  171.             case '+' : theOperator = Add; break;
  172.             case '-' : theOperator = Subtract; break;
  173.             case '~' : theOperator = RevSubtract; break;
  174.             case '*' : theOperator = Multiply; break;
  175.             case '/' : theOperator = Divide; break;
  176.             case '\\' : theOperator = RevDivide; break;
  177.             case '&' : theOperator = And; break;
  178.             case '|' : theOperator = Or; break;
  179.             case '!' : theOperator = Not; break;
  180.             default  : theOperator = Illegal; break;
  181.             }
  182.         (*position)++;
  183.         }
  184.         
  185.     ch = theSegment[*position];
  186.     
  187.     while(isalnum(ch) || (ch == '%') || (ch == '$'))    
  188.         /* read in item */ 
  189.         {
  190.         theResult[x] = ch;
  191.         (*position)++; 
  192.         x++;
  193.         ch = theSegment[*position];
  194.         }
  195.         
  196.     theResult[x] = '\0';                                /* terminate string */
  197.     
  198.     return(theOperator);
  199.     }
  200.     
  201. void nextLine(sHandle sourceCode)
  202.     {
  203.     char **startHandle;
  204.     char ch;
  205.     long caretPos,size;
  206.  
  207.  
  208.     startHandle = (*sourceCode)->storage; 
  209.     caretPos = (*sourceCode)->caret;
  210.     size = (*sourceCode)->size;
  211.     
  212.     while(((*startHandle)[caretPos] != '\r') && (caretPos <= size))
  213.         caretPos++;
  214.     
  215.     
  216.     if ((*startHandle)[caretPos] == '\0')
  217.         AsmError = UnexpectedEndOfFile;    
  218.     else
  219.         (*sourceCode)->line++;
  220.     
  221.     caretPos++;
  222.     
  223.     (*sourceCode)->storage = startHandle; 
  224.     (*sourceCode)->caret = caretPos;
  225.     }    
  226.  
  227. /*   private  */
  228.  
  229.  
  230.  
  231.  
  232.  
  233.  
  234.  
  235.  
  236.  
  237.